package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.AreaManagement;
import com.example.prison.entity.DefenceArea;
import com.example.prison.mapper.AreaManagementMapper;
import com.example.prison.service.DefenceAreaService;
import com.example.prison.mapper.DefenceAreaMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
* @author jiang
* @description 针对表【defence_area】的数据库操作Service实现
* @createDate 2023-06-02 12:33:20
*/
@Service
public class DefenceAreaServiceImpl extends ServiceImpl<DefenceAreaMapper, DefenceArea>
    implements DefenceAreaService{

    @Autowired
    private DefenceAreaMapper defenceAreaMapper;

    @Autowired
    private AreaManagementMapper areaManagementMapper;

    @Override
    public PageInfo<DefenceArea> get(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(defenceAreaMapper.get());
    }

    @Override
    public int addSentryPost(DefenceArea area) {
        if (ObjectUtils.isNotEmpty(defenceAreaMapper.getByNumber(area.getNumber()))) {
            return -1;
        }
//        String sentryPost = area.getNumber();
//        if (ObjectUtils.isNotEmpty(areaManagementMapper.getOneBySentryPost(sentryPost))) {
//            return -1;
//        } else {
            defenceAreaMapper.add(area);
//            AreaManagement areaManagement = new AreaManagement();
//            areaManagement.setSentryPost(area.getNumber());
//            areaManagement.setSentryAddress(area.getAddress());
//            areaManagement.setStatus(area.getStatus());
//            areaManagementMapper.add(areaManagement);
            return 1;
//        }
    }

    @Override
    public int addDefenceArea(DefenceArea area) {
        if (ObjectUtils.isNotEmpty(defenceAreaMapper.getByNumber(area.getNumber()))) {
            return -1;
        }
        String defenseSection = area.getNumber();
        if (ObjectUtils.isNotEmpty(areaManagementMapper.getOneByDefenseSection(defenseSection))) {
            return 0;
        } else {
            defenceAreaMapper.add(area);
            AreaManagement areaManagement = new AreaManagement();
            areaManagement.setDefenseSection(area.getNumber());
            areaManagement.setDefenseAddress(area.getAddress());
            areaManagement.setDefenseSectionPoint(area.getAddress());
            areaManagement.setStatus(area.getStatus());
            areaManagementMapper.add(areaManagement);
            return 1;
        }
    }

    @Override
    public Integer deleteDefenceArea(Integer[] ids) {
        for (Integer id : ids) {
            if(ObjectUtils.isEmpty(defenceAreaMapper.getById(id))) {
                return -1;
            }
            DefenceArea defenceArea = defenceAreaMapper.getById(id);
            if(ObjectUtils.isEmpty(areaManagementMapper.getOneByDefenseSection(defenceArea.getNumber()))) {
                defenceAreaMapper.deleteDefenceAreaById(id);
                return 0;
            }
            AreaManagement area = areaManagementMapper.getOneByDefenseSection(defenceArea.getNumber());
            areaManagementMapper.deleteOneById(area.getId());
        }
        defenceAreaMapper.deleteDefenceArea(ids);
        return 1;
    }

    @Override
    public Integer deleteSentryPost(Integer[] ids) {
        for (Integer id : ids) {
            if(ObjectUtils.isEmpty(defenceAreaMapper.getById(id)) ) {
                return -1;
            }
//            DefenceArea defenceArea = defenceAreaMapper.getById(id);
//            if(ObjectUtils.isEmpty(areaManagementMapper.getOneBySentryPost(defenceArea.getNumber()))) {
//                defenceAreaMapper.deleteDefenceAreaById(id);
//                return 0;
//            }
//            AreaManagement area = areaManagementMapper.getOneBySentryPost(defenceArea.getNumber());
//            areaManagementMapper.deleteOneById(area.getId());
        }
        defenceAreaMapper.deleteDefenceArea(ids);
        return 1;
    }

    @Override
    public Integer updateAreaStatus(Integer id, DefenceArea area) {
        if(ObjectUtils.isEmpty(defenceAreaMapper.getById(id))) {
            return 0;
        }
        DefenceArea deDTO = defenceAreaMapper.getById(id);
        defenceAreaMapper.updateDefenceArea(area,id);
        List<DefenceArea> areaList = defenceAreaMapper.getDefenceAreaByNumber(area.getNumber());
        if(areaList.size() > 1) {
            defenceAreaMapper.updateDefenceAreaFail(deDTO, id);
            return -1;
        }
        if(ObjectUtils.isNotEmpty(areaManagementMapper.getOneByDefenseSection(deDTO.getNumber()) )) {
            //防区段
            AreaManagement areaManagement = areaManagementMapper.getOneByDefenseSection(deDTO.getNumber());
            areaManagement.setDefenseSection(area.getNumber());
            areaManagement.setDefenseAddress(area.getAddress());
            areaManagement.setStatus(area.getStatus());
            areaManagementMapper.updateArea(areaManagement, areaManagement.getId());
        }
        return 1;
    }

    @Override
    public PageInfo<DefenceArea> getByNumber(int pageNumber, int pageSize, String defenseSection) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(defenceAreaMapper.getByNumber(defenseSection));
    }

    @Override
    public PageInfo<DefenceArea> getByStatus(int pageNumber, int pageSize, String defenseSection) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(defenceAreaMapper.getByStatus(defenseSection));
    }
}




